/*
 * Rank sorting in sorting OpenCL
 * This kernel has a bug. What?
 */

__kernel void sort(__global unsigned int *data, const unsigned int length, __global unsigned int *output)
{ 
  __local unsigned int myBuffer[64*32];
  unsigned int pos = 0;
  unsigned int i;
  unsigned int val;

  //for (i = get_local_id(0); i < get_global_size(0); i=i+16) {
    //  myBuffer[i] = data[i];
  //}  

  //barrier(CLK_LOCAL_MEM_FENCE);
  //val=myBuffer[get_global_id(0)]; 

  val=data[get_global_id(0)]; 

  //find out how many values are smaller
  for (i = 0; i < get_global_size(0); i++) {
    //if (val > data[i]) {
      // pos++;		       
     // }         
    if (val > data[i]) {
       pos++;		       
    }  
  }     
  output[pos]=val;
}

